package com.woniuxy.bookmallmanagersrever.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmallmanagersrever.entity.TManager;
import com.woniuxy.bookmallmanagersrever.mapper.TManagerMapper;
import com.woniuxy.bookmallmanagersrever.mapper.TManagerRoleMapper;
import com.woniuxy.bookmallmanagersrever.vo.ManagerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ManagerService {
    @Autowired
    TManagerMapper managerMapper ;
    @Autowired
    TManagerRoleMapper managerRoleMapper ;

    public TManager login(String username) {
        return managerMapper.selectManagerByName(username);
    }

    public PageInfo<ManagerVo> findManagerByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now,size);
        List<ManagerVo> lists = managerMapper.selectManagerByCons(name);
        return new PageInfo<>(lists);
    }

    @Transactional
    public void add(ManagerVo managerVo) {
        TManager manager = new TManager();
        manager.setCellphone(managerVo.getCellphone());
        manager.setUserName(managerVo.getUserName());
        manager.setEmail(managerVo.getEmail());
        manager.setLocked("0");
        manager.setUserPassword("123456");
        manager.setInsertTime(new Date());

        managerMapper.insert(manager) ; //

        int[] roleIds = managerVo.getRoleIds();
        if (roleIds != null && roleIds.length != 0) {
            //维护关系表
            managerRoleMapper.insertBatch(manager.getId(),roleIds);
        }
    }
}
